package app.utils;

import java.io.File;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Common {

	// 网站验证码在session中保存的名称
	// VerificationCode中使用了此名称
	public static String sessionVerificationCode = "sessionSecurityName";

	/***
	 * session中保存的用户登录后的信息名称 user
	 */
	public static String sessionUser = "user";

	// 网站标题中显示的网站名称（应用名称）
	public static String appName = "智能物联";// "智能物联平台";
	// 网站标题中显示的网站名称（应用名称）
	public static String appSubName = "appname";
	// 网站标题中显示的网站名称（应用名称）
	public static String companyName = "四川昊拓科技有限公司";
	// log4j基础配置
	private static Logger logger = LogManager.getLogger("Common");

	/**
	 * 页面提示信息出现在注册、登录界面中
	 */
	public Common() {
	}

	public static int getBrowser(HttpServletRequest request, HttpServletResponse response) {

		// user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36
		// (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
		// user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36
		// (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36
		//
		// user-agent=Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64;
		// Trident/8.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR
		// 3.0.30729; .NET CLR 3.5.30729)
		//
		// user-agent=Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64;
		// Trident/8.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR
		// 3.0.30729; .NET CLR 3.5.30729; GreenBrowser)
		//
		// user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0)
		// like Gecko
		String agent = request.getHeader("user-agent");
		int version = 0;
		try {
			version = Integer.parseInt(agent.substring(agent.indexOf('/') + 1, agent.indexOf('.')));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return version;
	}

	/*****
	 * 在过滤器中获取完整url
	 * 
	 * @return
	 */
	public static String getUrlInFilter(HttpServletRequest request, HttpServletResponse response) {
		// 完整url
		HttpServletRequest req = (HttpServletRequest) request;
		String url = req.getRequestURI();
		if (req.getQueryString() != null) {
			url += "?" + req.getQueryString();// 参数
		}
		return url;
	}

	// control.java引用
	public static String getRemoteIP(HttpServletRequest request, HttpServletResponse response) {

		String ip = "127.0.0.1";
		try {
			ip = request.getHeader("x-forwarded-for");
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getHeader("WL-Proxy-Client-IP");
			}
			if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip = request.getRemoteAddr();
			}
			if (ip.equals("0:0:0:0:0:0:0:1")) {
				ip = "127.0.0.1";
			}
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return ip;
	}

	// control.java引用
	public static String getUrlInServlet(HttpServletRequest request, HttpServletResponse response) {
		// 完整url
		String url = request.getRequestURI();
		String qs = request.getQueryString();
		if (StringUtils.isBlank(qs)) {
			url += "?" + qs;// 参数
		}
		return url;
	}

	public String getCurrentBasePath(HttpServletRequest request, HttpServletResponse response) {
		// 完整url
		String path = request.getContextPath();
		// System.out.println("Common.getBasePath()request.getContextPath()="+request.getContextPath());
		// System.out.println("Common.getBasePath()request.getScheme()="+request.getScheme());
		// System.out.println("Common.getBasePath()request.getServerName()="+request.getServerName());
		// System.out.println("Common.getBasePath()request.getServerPort()="+request.getServerPort());
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path
				+ "/";
		return basePath;
	}

	public static String getUrl(HttpServletRequest request, HttpServletResponse response) {
		String url = request.getRequestURI();
		if (request.getQueryString() != null) {
			url += "?" + (request.getQueryString());// 参数
		}
		return url;
	}

	// 上传文件时，保存文件
	public String uploadfile(String fileNamePath, String savefolder, String fileName) {
		// 写到指定的路径中
		File filefolder = new File(savefolder);
		// 如果指定的路径没有就创建
		if (!filefolder.exists()) {
			filefolder.mkdirs();
		}
		File file = new File(fileNamePath);
		File savefile = new File(savefolder, fileName);
		try {

			FileUtils.copyFile(file, savefile);

		} catch (Exception e) {
			return null;
		}
		return savefile.getAbsolutePath();
	}

}
